// controller
Ext.regController("Album", {
    id: "Album",
    // index controller
    index: function() {
        
        /*Ext.getCmp("topBar").remove("back-share");
        Ext.getCmp("topBar").insert(0,{
            title:"Share",
            id:"share-pix",
            iconCls:"compose",
            iconMask: true,
            cls: "upload",
            handler:function(){
                Ext.redirect("Upload/index");
            }
        });
        Ext.getCmp("topBar").doLayout();*/
        
        if (!this.listPanel) {
            this.listPanel = this.render({
                xtype: 'gallery-albumlistpanel',
                listeners: {
                    list: {
                        select: function(list, record) {
										 console.log(record);
                            this.viewList(list, record, 'left');
                        },
                        scope: this
                    },
                    activate : function(listPanel) {
                        listPanel.list.getSelectionModel().deselectAll();
                    }
                }
            });
            
            Gallery.viewport.setActiveItem(this.listPanel);
        } else {
            Gallery.viewport.setActiveItem(this.listPanel, {
                type: 'slide',
                direction: 'right'
            });
        }
    },
    // viewList controller
    viewList: function(list, record, slide) {  
        
        var albumView = this.render({
            xtype: 'gallery-albumviewpanel',
            listeners: {
                activate : function(albumView) {
                  console.log("loaded dataview");
                  
                    this.storImage = new Ext.data.Store({
                        model: "Image"
                    });
					
									
									
				   this.storImage.proxy.url = Gallery.urlserver+'/api?action=viewAlbum';
									
									
                    this.storImage.load({
                        params:{
                            aid: record.data.id,
                            lg: Gallery.userlogin,
                            uuid: Gallery.useruuid
                        },
                        callback:function(rec){
                            console.log(rec);
                        }
                    });
                    
                    
                    
                   albumView.dataView.bindStore(this.storImage);
                   albumView.dataView.refresh();
                   
                   console.log("bindstore");
                },
                deactivate: function(albumView) {
                    albumView.destroy();
                },
                dataView: {
                    itemtap: function(list, index) {
                        var imgdata = albumView.dataView.store.data.items;
                        
                        this.viewCarousel(list, index, imgdata, record);
                    },
                    itemswipe: function(list, index) {
                        var imgdata = albumView.dataView.store.data.items;
                        albumView.update(
                            '<div style="background:url(' + 
                                imgdata[index].data.full + ') no-repeat;'+ 
                                'width:320px;height:640px;"></div>'
                        );
                    },
                    scope: this
                }
            }
        });
       
        albumView.query('#homeButton')[0].on({
            tap: this.index,
            scope: this
        });
        
        albumView.query('#albumToolbar')[0].setTitle(record.data.name);      
        
        Gallery.viewport.dockedItems.get("topBar").add([{
                    itemId: 'backButton',
                    ui: 'back',
                    text: 'Back',
                    iconMask: true
                }]);
        
        Gallery.viewport.setActiveItem(albumView, {
            type: 'slide',
            direction: 'slide'
        });
    },
    // viewCarousel controller
    viewCarousel: function(list, index, imgdata, album) {
        var albumCarousel = this.render({
            xtype: 'gallery-albumcarouselpanel',
            listeners: {
                deactivate: function() {
                    albumCarousel.destroy();
                }
            }
        });
        
        for(i=0; i<imgdata.length; i++) {
            console.log(imgdata[i].data);
            albumCarousel.carousel.add({
                html: '<div class="img"><img src="'+Gallery.urlserver+imgdata[i].data.full+'" /></div>'
            });
        }
        
        albumCarousel.query('#homeButton')[0].on({
            tap: this.index,
            scope: this
        });

        albumCarousel.query('#backButton')[0].on({
            tap: function() {
                this.viewList(list, album, 'right');
            },
            scope: this
        })
        
        Gallery.viewport.setActiveItem(albumCarousel, 'slide');
        albumCarousel.carousel.setActiveItem(index);
    }
});